####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Table A7
###
####################################################################################


library(AER)

################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))


################################################################################
# Auxiliary Functions
################################################################################

results = function(mod, mod_cl){
  print(round(mod_cl[c('postT', 'postT:factor(imm_quant_99)2', 'postT:factor(imm_quant_99)3'), 1:2], 3))
  print(paste0("Observations: ", attr(mod_cl, 'nobs')))
  print(paste0("R-squared: ", round(summary(mod)$r.squared, 3)))
}

################################################################################
#  Analysis
###############################################################################

# Sample and baseline regression
df1 = df_did[which(!is.na(df_did$fn)),]
basereg = "fn ~ postT*factor(imm_quant_99) + res_owned_share_90*policy_period + hlm_share_90*policy_period + imm_share_90*policy_period + running + factor(CODGEO) + factor(policy_period)"


# Column 1: no moderators for comparison 
print('------------- Column 1 --------------')
mod = lm(data=df1, as.formula(basereg))
mod_cl = coeftest(mod, vcov = vcovCL, cluster = ~df1$CODGEO)
results(mod, mod_cl)


# Column 2: adding income
print('------------- Column 2 --------------')
mod = lm(data=df1, paste0(basereg, paste0('+ postT*factor(', c('inc_quant'), ')', collapse = '')))
mod_cl = coeftest(mod, vcov = vcovCL, cluster = ~df1$CODGEO)
results(mod, mod_cl)

# Column 3: adding income, public housing
print('------------- Column 3 --------------')
mod = lm(data=df1, paste0(basereg, 
                           paste0('+ postT*factor(', c('inc_quant', 'hlm_quant'), ')', collapse = '')))
mod_cl = coeftest(mod, vcov = vcovCL, cluster = ~df1$CODGEO)
results(mod, mod_cl)


# Column 4: adding income, public housing, home ownership
print('------------- Column 4 --------------')
mod = lm(data=df1, paste0(basereg, 
                           paste0('+ postT*factor(', c('inc_quant', 'hlm_quant', 'res_quant'), ')', collapse = '')))
mod_cl = coeftest(mod, vcov = vcovCL, cluster = ~df1$CODGEO)
results(mod, mod_cl)


# Column 5: adding income, public housing, home ownership, associations
print('------------- Column 5 --------------')
mod = lm(data=df1, paste0(basereg, 
                           paste0('+ postT*factor(', c('inc_quant', 'hlm_quant', 'res_quant', 'asso_quant'), ')', collapse = '')))
mod_cl = coeftest(mod, vcov = vcovCL, cluster = ~df1$CODGEO)
results(mod, mod_cl)


# Column 6: adding income, public housing, home ownership, associations, education
print('------------- Column 6 --------------')
mod = lm(data=df1, paste0(basereg, 
                           paste0('+ postT*factor(', c('inc_quant', 'hlm_quant', 'res_quant', 'asso_quant', 'higheduc_quant'), ')', collapse = '')))
mod_cl = coeftest(mod, vcov = vcovCL, cluster = ~df1$CODGEO)
results(mod, mod_cl)


# Column 7: adding income, public housing, home ownership,  associations, education, congestion
print('------------- Column 7 --------------')
mod = lm(data=df1, paste0(basereg, 
                           paste0('+ postT*factor(', c('inc_quant', 'hlm_quant', 'res_quant', 'asso_quant', 'higheduc_quant', 'congestion'), ')', collapse = '')))
mod_cl = coeftest(mod, vcov = vcovCL, cluster = ~df1$CODGEO)
results(mod, mod_cl)


# Column 8: adding income, public housing, home ownership,  associations, education, congestion, FN vote
print('------------- Column 8 --------------')
mod = lm(data=df1, paste0(basereg, 
                           paste0('+ postT*factor(', c('inc_quant', 'hlm_quant', 'res_quant', 'asso_quant', 'higheduc_quant', 'fn_quant', 'congestion'), ')', collapse = '')))
mod_cl = coeftest(mod, vcov = vcovCL, cluster = ~df1$CODGEO)
results(mod, mod_cl)

